package com.ruoyi.login.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.login.entity.LoginVo;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/app/login")
public class LoginControllerApp {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysLoginService loginService;
    @Resource
    private AuthenticationManager authenticationManager;

//    @PostMapping("/login")
//    public AjaxResult login(@RequestBody LoginBody loginBody)
//    {
//        AjaxResult ajax = AjaxResult.success();
//        // 生成令牌
//        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
//                loginBody.getUuid());
//        ajax.put(Constants.TOKEN, token);
//        return ajax;
//    }

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        Authentication authentication = null;
        try {
            authentication = authenticationManager.
                    authenticate(new UsernamePasswordAuthenticationToken(loginBody.getUsername(), loginBody.getPassword()));
        }catch (Exception e){
            return AjaxResult.error("用户名密码错误");
        }
        return AjaxResult.success();
    }

    @GetMapping("/value")
    public LoginVo getValue(){
        LoginVo loginVo = new LoginVo();
        loginVo.setValue("111");
        return loginVo;
    }

}
